/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TAIHE_AUDIO_VOLUME_GROUP_MANAGER_H
#define TAIHE_AUDIO_VOLUME_GROUP_MANAGER_H

#include "audio_system_manager.h"
#include "taihe_work.h"

namespace ANI::Audio {
using namespace taihe;
using namespace ohos::multimedia::audio;

class AudioVolumeGroupManagerImpl {
public:
    AudioVolumeGroupManagerImpl();
    explicit AudioVolumeGroupManagerImpl(std::shared_ptr<AudioVolumeGroupManagerImpl> obj);

    static AudioVolumeGroupManager CreateAudioVolumeGroupManagerWrapper(int32_t groupId);

    void SetVolumeSync(AudioVolumeType volumeType, int32_t volume);
    void SetVolumeWithFlagSync(AudioVolumeType volumeType, int32_t volume, int32_t flags);
    AudioVolumeType GetActiveVolumeTypeSync(int32_t uid);
    int32_t GetVolumeSync(AudioVolumeType volumeType);
    int32_t GetMinVolumeSync(AudioVolumeType volumeType);
    int32_t GetMaxVolumeSync(AudioVolumeType volumeType);
    void MuteSync(AudioVolumeType volumeType, bool mute);
    bool IsMuteSync(AudioVolumeType volumeType);
    void SetRingerModeSync(AudioRingMode mode);
    AudioRingMode GetRingerModeSync();
    void SetMicMuteSync(bool mute);
    void SetMicMutePersistentSync(bool mute, PolicyType type);
    bool IsPersistentMicMute();
    bool IsMicrophoneMuteSync();
    void AdjustVolumeByStepSync(VolumeAdjustType adjustType);
    bool IsVolumeUnadjustable();
    void AdjustSystemVolumeByStepSync(AudioVolumeType volumeType, VolumeAdjustType adjustType);
    double GetSystemVolumeInDbSync(AudioVolumeType volumeType, int32_t volumeLevel, DeviceType device);
    double GetMaxAmplitudeForInputDeviceSync(AudioDeviceDescriptor inputDevice);
    double GetMaxAmplitudeForOutputDeviceSync(AudioDeviceDescriptor inputDevice);
    void OnRingerModeChange(callback_view<void(AudioRingMode)> callback);
    void OnMicStateChange(callback_view<void(MicStateChangeEvent const&)> callback);
    void OffRingerModeChange(optional_view<callback<void(AudioRingMode)>> callback);
    void OffMicStateChange(optional_view<callback<void(MicStateChangeEvent const&)>> callback);

private:
    static void RegisterRingModeCallback(std::shared_ptr<uintptr_t> &callback,
        const std::string &cbName, AudioVolumeGroupManagerImpl *audioVolumeGroupManagerImpl);
    static void RegisterMicStateChangeCallback(std::shared_ptr<uintptr_t> &callback,
        const std::string &cbName, AudioVolumeGroupManagerImpl *audioVolumeGroupManagerImpl);
    static void UnregisterRingerModeCallback(std::shared_ptr<uintptr_t> &callback,
        AudioVolumeGroupManagerImpl *audioVolumeGroupManagerImpl);
    static void UnregisterMicStateChangeCallback(std::shared_ptr<uintptr_t> &callback,
        AudioVolumeGroupManagerImpl *audioVolumeGroupManagerImpl);

    std::shared_ptr<OHOS::AudioStandard::AudioGroupManager> audioGroupMngr_ = nullptr;
    int32_t cachedClientId_ = -1;

    std::shared_ptr<OHOS::AudioStandard::AudioRingerModeCallback> ringerModecallbackTaihe_ = nullptr;
    std::shared_ptr<OHOS::AudioStandard::AudioManagerMicStateChangeCallback> micStateChangeCallbackTaihe_ = nullptr;
    std::mutex mutex_;
};
} // namespace ANI::Audio

#endif // TAIHE_AUDIO_VOLUME_GROUP_MANAGER_H
